package com.kejiguan.ui;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast;

import com.kejiguan.ui.adapter.UserInfoMenuAdapter;
import com.kejiguan.ui.dialog.CommDialogUtil;
import com.kejiguan.ui.dialog.ModifyUserPassDialog;
import com.kejiguan.ui.dialog.ModifyUserPassDialogUtil;
import com.kejiguan.ui.model.MenuItem;
import com.kejiguan.ui.presenter.UserInfoFragmentPresenter;
import com.kejiguan.ui.response.ClearWorkOrderResponse;
import com.kejiguan.ui.response.ModifyUserPassResponse;
import com.kejiguan.ui.util.CacheDataUtil;
import com.kejiguan.ui.util.MD5Encrypt;
import com.kejiguan.ui.widget.NoScrollGridView;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;
import java.util.List;

/**
 * Created by YYY on 2016/4/30.
 */
public class UserInfoFragment extends Fragment implements View.OnClickListener, ModifyUserPassDialog.OnNewPassListener {

    private View rootView;

    private View detailView;
    private TextView userNameView;
    private TextView userTelView;
    private TextView userEmailView;
    private View modifyUserPaswBtn;
    private View clearBtn;
    private NoScrollGridView menuList;
    private UserInfoMenuAdapter menuAdapter;


    private List<MenuItem> menuItems;
    private UserInfoFragmentPresenter presenter;
    @Override
    public void onCreate(Bundle bundle) {
        super.onCreate(bundle);
        EventBus.getDefault().register(this);
        presenter=new UserInfoFragmentPresenter();
        presenter.attach(getContext());
    }

    @Override
    public void onDestroy()
    {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }

    @Override
    public final View onCreateView(LayoutInflater inflater, ViewGroup container,
                                   Bundle savedInstanceState) {

        if (null == rootView) {
            rootView = LayoutInflater.from(getContext()).inflate(R.layout.fragment_user_info_layout, container, false);
            detailView = rootView.findViewById(R.id.detail_view);
            userNameView = (TextView) rootView.findViewById(R.id.user_name);
            userTelView = (TextView) rootView.findViewById(R.id.user_tel);
            userEmailView = (TextView) rootView.findViewById(R.id.user_email);
            modifyUserPaswBtn = rootView.findViewById(R.id.modify_user_password_btn);
            clearBtn=rootView.findViewById(R.id.clear);
            menuList = (NoScrollGridView) rootView.findViewById(R.id.menu_list);
            modifyUserPaswBtn.setOnClickListener(this);
            clearBtn.setOnClickListener(this);

            menuAdapter = new UserInfoMenuAdapter(getContext(), CacheDataUtil.getInstance().getMenuItems());
            menuList.setAdapter(menuAdapter);

            userNameView.setText(CacheDataUtil.getInstance().getmUser().getUserName());
            userEmailView.setText(TextUtils.isEmpty(CacheDataUtil.getInstance().getmUser().getEmail())?"无":CacheDataUtil.getInstance().getmUser().getEmail());
            userTelView.setText(TextUtils.isEmpty(CacheDataUtil.getInstance().getmUser().getTelephone())?"无":CacheDataUtil.getInstance().getmUser().getTelephone());

        }
        return rootView;
    }

    @Override
    public void onClick(View v) {

        switch (v.getId()) {
            case R.id.modify_user_password_btn:
                ModifyUserPassDialogUtil.getInstance().show(getContext(), this);
                break;
            case R.id.clear:
                CommDialogUtil.getInstance().show(getContext(),"清除所有工单?",UserInfoFragment.this,"清除","取消",0);
                break;
            case R.id.dialog_confirm_btn:
                CommDialogUtil.getInstance().dismiss();
                presenter.clearWorkOrder();
                break;
            case R.id.dialog_cancel_btn:
                CommDialogUtil.getInstance().dismiss();
                break;
        }
    }

    @Override
    public void newPass(String pass) {

        String password= MD5Encrypt.MD5Encode(pass);
        presenter.modifyUser(String.valueOf(CacheDataUtil.getInstance().getmUser().getUserId()),password);

    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onEventMainThread(ModifyUserPassResponse response) {
        if ("0000".equals(response.getRetCode())) {
            Toast.makeText(getContext(), "修改密码成功", Toast.LENGTH_SHORT).show();
            Intent intent=new Intent(getContext(),LoginActivity.class);
            startActivity(intent);
            ( (Activity)getContext()).finish();
        } else {
            Toast.makeText(getContext(), response.getRetInfo(), Toast.LENGTH_SHORT).show();
        }
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onEventMainThread(ClearWorkOrderResponse response) {
        if ("0000".equals(response.getRetCode())) {
            Toast.makeText(getContext(), "清除工单成功!", Toast.LENGTH_SHORT).show();
        } else {
            Toast.makeText(getContext(), response.getRetInfo(), Toast.LENGTH_SHORT).show();
        }
    }




}
